package com.example.app.listener;

import com.example.app.service.WebSocketService;
import jakarta.annotation.Resource;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MessageEventListener {

    @Resource
    private WebSocketService webSocketService;

    @EventListener
    public void handleNewMessage(NewMessageEvent event) {
        // 推送新消息通知给接收方
        webSocketService.sendMessageToUser(
                event.getReceiverId(), "new_message",
                Map.of(
                        "type", "new_message",
                        "data", event.getMessage()
                )
        );

        // 可以继续推送会话更新
        webSocketService.sendMessageToUser(
                event.getReceiverId(), "conversation_updated",
                Map.of(
                        "type", "conversation_updated",
                        "conversationId", event.getConversationId(),
                        "lastMessage", event.getMessage().getContent(),
                        "timestamp", event.getMessage().getSentAt()
                )
        );
    }
}